# Find the Bristol Tracking Library
# uses $ENV{BTL} as a hint to help find the library

if (DEFINED BTL_FOUND)
   if (BTL_FIND_REQUIRED AND (NOT BTL_FOUND))
      message(SEND_ERROR "Could not find BTL")
   endif()
   return()
endif()

find_package(Boost 1.43 REQUIRED)
find_package(BoostNumericBindings REQUIRED)

set(BTL_ROOT "$ENV{BTL}" CACHE PATH "The root for BTL libraries.")

# set up build environment
set(BTL_INCLUDE_DIRS
   ${Boost_INCLUDE_DIRS}
   ${BoostNumericBindings_INCLUDE_DIRS}

   ${BTL_ROOT}/include
   ${BTL_ROOT}/include/include
   ${BTL_ROOT}/include/common
   ${BTL_ROOT}/include/Math
   ${BTL_ROOT}/include/Math/SmallMathLib
   ${BTL_ROOT}/include/Math/MathUtils
   ${BTL_ROOT}/include/ChiSquareDistribution
   ${BTL_ROOT}/include/random
   ${BTL_ROOT}/include/ransac/include
   ${BTL_ROOT}/include/ImageProcessing
   ${BTL_ROOT}/include/descriptors/MultiResolutionDescriptor/src
   ${BTL_ROOT}/include/descriptors/MultiResolutionDescriptor/CDLib
   ${BTL_ROOT}/include/descriptors/sah
   ${BTL_ROOT}/include/VideoSource/src
   ${BTL_ROOT}/include/CameraGeometry
   ${BTL_ROOT}/include/PoseEstimation
   ${BTL_ROOT}/include/slam-system/src
   ${BTL_ROOT}/include/StochasticFilters
   ${BTL_ROOT}/include/StochasticFilters/OldKalmanFilters/include
)

set(X_BTL_LIBRARIES
   ${BTL_ROOT}/slam-system/BtlSlamSystem

   ${BTL_ROOT}/VideoSource/BtlVideoSource
   ${BTL_ROOT}/CameraGeometry/BtlCameraGeometry
   ${BTL_ROOT}/descriptors/MultiResolutionDescriptor/BtlMultiResDescriptor
   ${BTL_ROOT}/descriptors/sah/BtlSahDescriptor
   ${BTL_ROOT}/Profiler/Shiny/BtlShinyProfiler
   ${BTL_ROOT}/ImageProcessing/BtlImgProc

   ${BTL_ROOT}/StochasticFilters/OldKalmanFilters/BtlOldKalmanFilters
   ${BTL_ROOT}/StochasticFilters/BtlGeneralFilter
   ${BTL_ROOT}/PoseEstimation/BtlPoseEstimation

   ${BTL_ROOT}/Math/MathUtils/BtlMathUtils
   ${BTL_ROOT}/Math/SmallMathLib/BtlSmallMathLib
   ${BTL_ROOT}/Math/OldMathLib/BtlOldMathLib
   ${BTL_ROOT}/Config/BtlConfig
   ${BTL_ROOT}/ransac/BtlRansac
   ${BTL_ROOT}/ChiSquareDistribution/BtlChiSquareDistribution
   ${BTL_ROOT}/random/BtlRandom
)

set(MISSING_BTL_LIBS)
set(BTL_LIBRARIES)

set(BTL_FOUND TRUE)

foreach(X_BTL_LIB ${X_BTL_LIBRARIES})
	get_filename_component(X_BTL_LIB_PATH "${X_BTL_LIB}" PATH)
	get_filename_component(X_BTL_LIB_NAME "${X_BTL_LIB}" NAME)

	find_library(BTL_${X_BTL_LIB_NAME}_LIBRARY
		NAMES "${X_BTL_LIB_NAME}"
		PATHS "${BTL_ROOT}" "${X_BTL_LIB_PATH}"
		PATH_SUFFIXES lib
		DOC "BTL ${X_BTL_LIB_NAME} library"
		NO_DEFAULT_PATH
	)

	if (NOT (BTL_${X_BTL_LIB_NAME}_LIBRARY))
		set(BTL_FOUND FALSE)
		list(APPEND MISSING_BTL_LIBS ${X_BTL_LIB_NAME})
	else()
		list(APPEND BTL_LIBRARIES ${BTL_${X_BTL_LIB_NAME}_LIBRARY})
	endif()
endforeach()

if (BTL_FOUND)
	message(STATUS "Found BTL")
else()
	message(WARNING "BTL not found, missing ${MISSING_BTL_LIBS}")
endif()

unset(MISSING_BTL_LIBS)
